package quq.missq.views;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import quq.missq.R;
import quq.missq.adapter.OnWheelChangedListener;
import quq.missq.adapter.WheelAdapter;

/**
 * Created by wcx on 2015/9/17.
 */
public class SelectTimeDialog extends Dialog implements OnWheelChangedListener, View.OnClickListener {
    private Context mContext;
    private Button btn_cancel;
    private Button btn_confirm;
    private WheelView wheel_tall;
    private TextView tv_content;
    private List<String> times=new ArrayList<>();

    public SelectTimeDialog(Context context) {
        super(context, R.style.picker_style);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_tall);
        initView();
        initData();
        initListener();
        setDialogSize(1d);
    }

    public void setDialogSize(double percent){
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.mystyle);  //添加动画
        Display play = window.getWindowManager().getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        play.getMetrics(metrics);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width=(int) ((metrics.widthPixels)*percent);
        params.height=params.WRAP_CONTENT;
        onWindowAttributesChanged(params);
        window.setAttributes(params);
    }


    private void initView() {
        btn_cancel = (Button) findViewById(R.id.btn_cancel);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);
        wheel_tall = (WheelView) findViewById(R.id.wheel_tall);
        tv_content = (TextView) findViewById(R.id.tv_content);
        setCanceledOnTouchOutside(true);
    }

    private void initListener() {
        wheel_tall.addChangingListener(this);
        btn_cancel.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);
    }

    private TimeButtonListener listener;

    public void setTimeButtonListener(TimeButtonListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                listener.onPositiveButton(this);
                break;
            case R.id.btn_cancel:
                listener.onNegativeButton(this);
                break;
            default:
                break;
        }
    }

    public String getTime(){
        return times.get(wheel_tall.getCurrentItem());
    }

    //140 ---200
    public interface TimeButtonListener {
        void onPositiveButton(SelectTimeDialog dialog);
        void onNegativeButton(SelectTimeDialog dialog);
    }

    private void initData() {
        tv_content.setText("入学时间");
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        String nowYear=format.format(new Date());

        int inowYear = Integer.parseInt(nowYear);
        Log.i("wcx",inowYear+"inowYear");
        for (int i =1995; i<=inowYear; i++) {
            times.add(i+"年");
        }
        wheel_tall.TEXT_SIZE = 40;
        wheel_tall.setCanSrcoll(times.size() > 1 ? true : false);
        wheel_tall.setAdapter(new SelectTallAdapter());
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {

    }
    class SelectTallAdapter implements WheelAdapter {
        @Override
        public int getItemsCount() {
            return times.size();
        }

        @Override
        public String getItem(int index) {
            return times.get(index);
        }

        @Override
        public int getMaximumLength() {
            return times.size() == 0 ? 1 : times.size();
        }
    }
}
